<template>
  <el-dialog
    :visible.sync="visible"
    :title="dataForm.details ? '详情' :(!dataForm.id ? $t('add') : $t('update'))"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="close()"
    :destroy-on-close="true"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'"
      label-position="right"
      :disabled="dataForm.details"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="航班号" prop="lineno">
              <el-input type="text" v-model="dataForm.lineno" :placeholder="disabled?'':'航班号'"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="航船类型">
              <SelectComponent
                  dictType="ship_time_type"
                  v-model="dataForm.shiptype"
                  :placeholder="disabled?'':'航船类型'"
                  ref="shiptype"
              >
              </SelectComponent>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="船名" prop="shipname">
              <el-input type="text" v-model="dataForm.shipname" :placeholder="disabled?'':'船名'"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="出发时间" prop="start">
              <el-date-picker style="width:100%" value-format="yyyy-MM-dd HH:mm:ss" v-model="dataForm.start" type="datetime" :placeholder="disabled?'':'出发时间'">
              </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="到达时间" prop="arrive">
              <el-date-picker style="width:100%" value-format="yyyy-MM-dd HH:mm:ss" v-model="dataForm.arrive" type="datetime" :placeholder="disabled?'':'到达时间'">
              </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="时刻表" prop="time">
              <el-input type="text" v-model="dataForm.time" :placeholder="disabled?'':'时刻表'"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="上船码头" prop="shipport">
              <el-input type="text" v-model="dataForm.shipport" :placeholder="disabled?'':'上船码头'"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上车码头" prop="busport">
              <el-input type="text" v-model="dataForm.busport" :placeholder="disabled?'':'上车码头'"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="提示信息" prop="tips">
              <el-input type="text" v-model="dataForm.tips" :placeholder="disabled?'':'提示信息'"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template slot="footer" v-if="!disabled">
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
import mixinRegionModule from "@/mixins/region-module";

export default {
  mixins: [mixinRegionModule],

  data() {
    return {
      visible: false,
      dataForm: {
        id: "",
        lineno:"",
        shiptype:"",
        shiptypename:"",
        shipname:"",
        start:"",
        arrive:"",
        time:"",
        shipport:"",
        busport:"",
        tips:""
      },
    };
  },
  props: { disabled: Boolean },
  computed: {
    dataRule() {
      return {
        
      };
    },
  },
  methods: {
    close() {
      this.dataForm = {
        id: "",
        lineno:"",
        shiptype:"",
        shiptypename:"",
        shipname:"",
        start:"",
        arrive:"",
        time:"",
        shipport:"",
        busport:"",
        tips:""
      };
      this.$emit("changeDisabled");
    },

    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get("/sys/isshiptime/" + this.dataForm.id)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
          this.$refs.shiptype.selectValue = this.dataForm.shiptype+'';
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }

          this.dataForm.shiptypename = this.$refs.shiptype.selectOptions.find(s=>s.dictValue==this.dataForm.shiptype).dictLabel||'';
          this.$http[!this.dataForm.id ? "post" : "put"](
            !this.dataForm.id ? "/sys/isshiptime/" : "/sys/isshiptime/",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
  }
};
</script>
